package cn.devkits.main;

import java.awt.AWTException;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import cn.devkits.format.json.ui.JsonFormatPane;
import cn.devkits.utils.DKSystemUtils;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 * 程序总入口
 *
 * @author liushaofeng
 * @DateTime 2018年7月17日 下午9:34:27
 * @Version 1.0.0.0
 */
public class App extends Application {

	public static SystemTray tray = null;
	public static TrayIcon trayIcon = null;
	private static Rectangle2D screenSize;

	@Override
	public void start(Stage primaryStage) {
		BorderPane p = new BorderPane();
		Text text = new Text("Hello FX");
		text.setFont(Font.font("Arial", 60));
		p.setCenter(text);

		primaryStage.setTitle("DevKits " + DKSystemUtils.getCurrentVersion());
		primaryStage.setScene(new Scene(p));
		screenSize = DKSystemUtils.getScreenSize();
		primaryStage.setWidth(screenSize.getMaxX() * 0.618);
		primaryStage.setHeight(screenSize.getMaxY() * 0.618);
		primaryStage.setResizable(true);

		regMenu(primaryStage);
		regContent(primaryStage);
		regStatusBar(primaryStage);
		regSystemTray();

		primaryStage.show();
	}

	private void regContent(Stage primaryStage) {
		// TODO Auto-generated method stub

	}

	private void regStatusBar(Stage primaryStage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void init() throws Exception {
		super.init();

		// 加载配置信息
		DKSystemUtils.init();
	}

	@Override
	public void stop() throws Exception {
		tray.remove(trayIcon);
		super.stop();
	}

	private void regMenu(Stage primaryStage) {

		MenuBar menuBar = new MenuBar();

		// --- Menu File
		Menu menuFile = new Menu("_File");
		menuFile.setMnemonicParsing(true);
		MenuItem openMenuItem = new MenuItem("Open...");
		openMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.SHORTCUT_DOWN));// Ctrl+O
		openMenuItem.setOnAction((ActionEvent t) -> {
			File file = new FileChooser().showOpenDialog(primaryStage);
			if (file != null) {
				Alert alert = new Alert(AlertType.INFORMATION);
				alert.setTitle("Fiel Chooser");
				alert.setHeaderText(null);
				alert.setContentText("In Developing...");
				alert.showAndWait();
			}
		});
		menuFile.getItems().add(openMenuItem);
		menuFile.getItems().add(new SeparatorMenuItem());
		MenuItem exitMenuItem = new MenuItem("Exit");
		exitMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.F4, KeyCombination.ALT_DOWN));// alt+F4
		exitMenuItem.setOnAction((ActionEvent t) -> {
			System.exit(0);
		});
		menuFile.getItems().add(exitMenuItem);

		// --- Menu Edit
		Menu menuEdit = new Menu("_Edit");
		MenuItem cutMenuItem = new MenuItem("Cut");
		cutMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.X, KeyCombination.SHORTCUT_DOWN));// Ctrl+X
		cutMenuItem.setOnAction((ActionEvent t) -> {

		});
		menuEdit.getItems().add(cutMenuItem);
		MenuItem copyMenuItem = new MenuItem("Copy");
		copyMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN));// Ctrl+C
		copyMenuItem.setOnAction((ActionEvent t) -> {

		});
		menuEdit.getItems().add(copyMenuItem);
		MenuItem pasteMenuItem = new MenuItem("Paste");
		pasteMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.V, KeyCombination.SHORTCUT_DOWN));// Ctrl+P
		pasteMenuItem.setOnAction((ActionEvent t) -> {

		});
		menuEdit.getItems().add(pasteMenuItem);
		menuEdit.getItems().add(new SeparatorMenuItem());
		MenuItem formatMenuItem = new MenuItem("Format");
		menuEdit.getItems().add(formatMenuItem);

		// --- Menu Tools
		Menu menuTools = new Menu("_Tools");
		MenuItem httpPostMenuItem = new MenuItem("HttpPost...");
		menuTools.getItems().add(httpPostMenuItem);
		menuTools.getItems().add(new SeparatorMenuItem());
		MenuItem fileWindowMenuItem = new MenuItem("File Window");
		menuTools.getItems().add(fileWindowMenuItem);
		menuTools.getItems().add(new SeparatorMenuItem());
		MenuItem nipicMenuItem = new MenuItem("Nipic Download...");
		menuTools.getItems().add(nipicMenuItem);

		// --- Menu Help
		Menu menuView = new Menu("_Help");
		MenuItem aboutMenuItem = new MenuItem("About");
		aboutMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.F1));// F1
		aboutMenuItem.setOnAction((ActionEvent t) -> {
			Alert alert = new Alert(AlertType.INFORMATION);
			alert.setTitle("About Devkits");
			String headerText = "DEVKITS.CN © 2018 All Rights Reserved" + System.getProperty("line.separator")
					+ "Version:" + DKSystemUtils.getCurrentVersion();
			alert.setHeaderText(headerText);

			alert.setContentText(DKSystemUtils.getOpenSourceInfo());

			alert.showAndWait();
		});
		menuView.getItems().add(aboutMenuItem);

		menuBar.getMenus().addAll(menuFile, menuEdit, menuTools, menuView);

		Scene scene = new Scene(menuBar);
		primaryStage.setScene(scene);
	}

	private static void regSystemTray() {
		if (SystemTray.isSupported()) {
			try {
				tray = SystemTray.getSystemTray();
				BufferedImage image = ImageIO.read(App.class.getResourceAsStream("/icons/tools_16.png"));

				trayIcon = new TrayIcon(image, "Develop Kits");
				trayIcon.setToolTip("Develop Kits");
				trayIcon.setImageAutoSize(true);

				PopupMenu popupMenu = new PopupMenu();

				java.awt.Menu settingsMenu = new java.awt.Menu("Settings");
				popupMenu.add(settingsMenu);

				popupMenu.addSeparator();
				java.awt.MenuItem exitItem = new java.awt.MenuItem("Exit");
				popupMenu.add(exitItem);

				trayIcon.setPopupMenu(popupMenu);

				tray.add(trayIcon);

				exitItem.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent e) {
						System.exit(0);
					}
				});
			} catch (AWTException e) {
				Logger.getLogger(App.class).error(e.getMessage(), e);
			} catch (IOException e) {
				Logger.getLogger(App.class).error(e.getMessage(), e);
			}
		}
	}

	public static void main(String[] args) {
		launch(args);
	}
}
